package presentation;

import java.awt.*;

import java.util.*;

import javax.swing.*;


public class Effects {
	static int slideSpeed = 20;
	
	public static void slideFromLeft(final JPanel panel, final JPanel operationPanel) {
		int count = operationPanel.getComponentCount();
		ArrayList<Component> list = new ArrayList<Component>();
		for (Component comp : operationPanel.getComponents()) {
			list.add(comp);
		}
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				Component comp = operationPanel.getComponent(i);

				if (comp instanceof JPanel) {
					final JPanel currentPanel = (JPanel) comp;
					if (currentPanel != panel) {

						new Thread() {

							public void run() {
								int x = -operationPanel.getWidth();

								
								for (int i = 0; i <= operationPanel.getWidth(); i += slideSpeed) {
									currentPanel.setBounds(i, 0,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									panel.setBounds(x+i, 0,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}

								operationPanel.remove(currentPanel);

								panel.setBounds(0, 0, operationPanel.getWidth(),operationPanel.getHeight());

							}
						}.start();
						break;
					}
				}
			}
		}

		if (!list.contains(panel)) {
			operationPanel.add(panel);
		}

		operationPanel.validate();
		operationPanel.repaint();
	}
	
	public static void slideFromRight(final JPanel panel,final JPanel operationPanel){
		int count = operationPanel.getComponentCount();
		ArrayList<Component> list = new ArrayList<Component>();
		for (Component comp : operationPanel.getComponents()) {
			list.add(comp);
		}
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				Component comp = operationPanel.getComponent(i);

				if (comp instanceof JPanel) {
					final JPanel currentPanel = (JPanel) comp;
					if (currentPanel != panel) {

						new Thread() {

							public void run() {
								int x = operationPanel.getWidth();

								for (int i = 0; i >= -operationPanel.getWidth(); i -= slideSpeed) {
									currentPanel.setBounds(i, 0,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									panel.setBounds(x+i, 0,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}

								operationPanel.remove(currentPanel);

								panel.setBounds(0, 0, operationPanel.getWidth(),operationPanel.getHeight());

							}
						}.start();
						break;
					}
				}
			}
		}

		if (!list.contains(panel)) {
			operationPanel.add(panel);
		}

		operationPanel.validate();
		operationPanel.repaint();
	}
	
	public static void slideFromTop(final JPanel panel,final JPanel operationPanel){
		int count = operationPanel.getComponentCount();
		ArrayList<Component> list = new ArrayList<Component>();
		for (Component comp : operationPanel.getComponents()) {
			list.add(comp);
		}
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				Component comp = operationPanel.getComponent(i);

				if (comp instanceof JPanel) {
					final JPanel currentPanel = (JPanel) comp;
					if (currentPanel != panel) {

						new Thread() {

							public void run() {
								int y = -operationPanel.getHeight();

								for (int i = 0; i <= -300+operationPanel.getWidth(); i += slideSpeed) {
									currentPanel.setBounds(0, i,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									panel.setBounds(0, y+i,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}

								operationPanel.remove(currentPanel);

								panel.setBounds(0, 0, operationPanel.getWidth(),operationPanel.getHeight());

							}
						}.start();
						break;
					}
				}
			}
		}

		if (!list.contains(panel)) {
			operationPanel.add(panel);
		}

		operationPanel.validate();
		operationPanel.repaint();
	}
	
	public static void slideFromBottom(final JPanel panel,final JPanel operationPanel){
		int count = operationPanel.getComponentCount();
		ArrayList<Component> list = new ArrayList<Component>();
		for (Component comp : operationPanel.getComponents()) {
			list.add(comp);
		}
		if (count > 0) {
			for (int i = 0; i < count; i++) {
				Component comp = operationPanel.getComponent(i);

				if (comp instanceof JPanel) {
					final JPanel currentPanel = (JPanel) comp;
					if (currentPanel != panel) {

						new Thread() {

							public void run() {
								int y = operationPanel.getHeight();

								for (int i = 0; i >= 300-operationPanel.getWidth(); i -= slideSpeed) {
									currentPanel.setBounds(0, i,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									panel.setBounds(0, y+i,
											operationPanel.getWidth(),
											operationPanel.getHeight());
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}

								operationPanel.remove(currentPanel);

								panel.setBounds(0, 0, operationPanel.getWidth(),operationPanel.getHeight());

							}
						}.start();
						break;
					}
				}
			}
		}

		if (!list.contains(panel)) {
			operationPanel.add(panel);
		}

		operationPanel.validate();
		operationPanel.repaint();
	}
}
